/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.InputStream;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaConfigurator;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.LyciaParserFactory;
import org.sourceprojects.lycia.ResourceResolver;
import org.sourceprojects.lycia.ResourceResolverFactory;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.test.impl.BaseBodyTagParser;
import org.sourceprojects.lycia.test.impl.InputTagParser;
import org.sourceprojects.lycia.test.impl.XHTMLTagParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class RemoveOnTransformTestCase {

	@Test
	public void testRemoveOnTransformTransforming()
			throws LyciaParsingException, TransformerException {
		final LyciaParser<StringBuilder> parser = LyciaParserFactory
				.newInstance();

		assertNotNull(parser);
		assertNotNull(parser.getParserContext());

		final ResourceResolver resolver = ResourceResolverFactory.newInstance();
		final StringBuilder buffer = new StringBuilder();
		assertNotNull(resolver);

		parser.getParserContext().setContextObject(buffer);
		parser.setResourceResolver(resolver);
		final LyciaConfigurator<StringBuilder> configurator = parser
				.getConfigurator();
		configurator.addTagDefinitionParser(new XHTMLTagParser());
		configurator.addTagDefinitionParser(new BaseBodyTagParser());
		configurator.addTagDefinitionParser(new InputTagParser());

		parser.getParserContext().setParserState("upload");

		final InputStream is = getClass().getClassLoader().getResourceAsStream(
				"test2.xml");

		final Document document = parser.transform(is);

		final TransformerFactory factory = TransformerFactory.newInstance();
		final Transformer transformer = factory.newTransformer();
		transformer.setOutputProperty("omit-xml-declaration", "yes");
		transformer.setOutputProperty(OutputKeys.METHOD, "xml");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty(
				"{http://xml.apache.org/xslt}indent-amount", "3");
		final DOMSource source = new DOMSource(document);
		final DOMResult result = new DOMResult();

		transformer.transform(source, result);

		final Node node = result.getNode().getFirstChild();
		assertEquals("body", node.getLocalName());
		for (int i = 0; i < node.getChildNodes().getLength(); i++) {
			final Node n = node.getChildNodes().item(i);
			System.out.println(n.getNodeType() + " " + n.getNodeName());
			assertEquals(Node.TEXT_NODE, n.getNodeType());
		}
	}
}
